package com.gs.wzweather.utils;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import org.apache.commons.net.ftp.FTPFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/6/21 0021
 */
public class FileSortUtil {

    public static void OrderByDate(SmbFile[] files){

        //按时间从最新在前排列
        Arrays.sort(files, new Comparator<SmbFile>(){
            public int compare(SmbFile f1, SmbFile f2) {
                long diff = 0;
                long sf1 = 0;
                long sf2 = 0;
                try {
                     sf1 = f1.lastModified();
                } catch (SmbException e) {
                    e.printStackTrace();
                }
                try {
                     sf2 = f2.lastModified();
                } catch (SmbException e) {
                    e.printStackTrace();
                }
                diff = sf1 - sf2;
                if (diff > 0)
                    return -1;
                else if (diff == 0)
                    return 0;
                else
                    return 1;
            }
        });
    }

    public static void OrderByDate(File[] files){
        //按时间从最新在前排列
        Arrays.sort(files, new Comparator<File>(){
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return -1;
                else if (diff == 0)
                    return 0;
                else
                    return 1;
            }
        });
    }

    public static void OrderByDate(FTPFile[] files){
        //按时间从最新在前排列
        Arrays.sort(files, new Comparator<FTPFile>(){
            public int compare(FTPFile f1, FTPFile f2) {
                long diff = f1.getTimestamp().getTimeInMillis() - f2.getTimestamp().getTimeInMillis();
                if (diff > 0)
                    return -1;
                else if (diff == 0)
                    return 0;
                else
                    return 1;
            }
        });
    }


}
